package cn.wyz.org.service.impl;

import cn.wyz.basic.exception.BusinessException;
import cn.wyz.basic.util.BaiduAuditUtils;
import cn.wyz.org.domain.Employee;
import cn.wyz.org.domain.Shop;
import cn.wyz.org.domain.ShopAuditLog;
import cn.wyz.org.mapper.EmployeeMapper;
import cn.wyz.org.mapper.ShopAuditLogMapper;
import cn.wyz.org.mapper.ShopMapper;
import cn.wyz.org.service.IEmployeeService;
import cn.wyz.org.service.IShopService;
import cn.wyz.basic.service.impl.BaseServiceImpl;
import cn.wyz.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WuyuZ
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper mapper;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 店铺入驻的逻辑:
     * 1.首先前端传回来的shop对象不能由空值
     * 2.对地址||店铺名等进行合法合规的检测
     *
     * 3.是否注册过?
     *     3.1 店铺是否已注册?
     *     3.2 管理员是否已注册?
     *       3.2.1注册了未绑定店铺
     *       3.2.2注册了也绑定了店铺(拒绝再注册)
     *
     * 4.保存数据
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();

//    * 1.首先前端传回来的shop对象不能有空值,具体哪些条目不能为空是我们自己的判断,只要有一个空,那么就抛回业务异常
        if(StringUtils.isEmpty(shop.getName()) ||
                StringUtils.isEmpty(shop.getAddress()) ||
                        StringUtils.isEmpty(shop.getTel()) ){
            throw new BusinessException("几个玩意儿不能为空!!");
        }
//    * 2.对地址||店铺名等进行合法合规的检测
        if(!BaiduAuditUtils.TextCensor(shop.getName()) || !BaiduAuditUtils.TextCensor(shop.getAddress())){
           throw new BusinessException("注意遣词用句！！！");
        }
//    * 3.是否注册过?
//    *     3.1 店铺是否已注册?
        Shop shopTmp = mapper.selectByName(shop.getName());
        if(shopTmp != null){
            throw new BusinessException("店铺已经被注册，请直接登录，如果忘记密码，请走找回密码流程！！！");
        }
//    *     3.2 管理员是否已注册? 这是是通过用户名来判断是否存在
        Employee etemp = employeeMapper.selectByUserName(admin.getUsername());
        if(etemp!=null && etemp.getShop_id()!=null){ //又能查出来,又有店铺id,表示这个人已经管理了一家
//    *       3.2.2注册了也绑定了店铺(拒绝再注册)
            throw new BusinessException("该用户已经存在运营店铺，请成功新换人！！");
        }
//    *       3.2.1注册了未绑定店铺 可以直接进行绑定,吧这一条与保存数据结合起来
        if(etemp==null){
            employeeService.insert(admin);//保存管理员信息,要返回自增id
            etemp = admin;//存进去以后不管etemp查出来有没有数据这里都有了
        }
//    * 4.保存数据
        shop.setAdminId(admin.getId());//把店铺的管理员id设置为这个返回的id
        mapper.insert(shop);//也返回自增id
        etemp.setShop_id(shop.getId());//反手把管理员那边的shop_id也进行一个设置,然后把这个新设置了的数据保存修改即可
        employeeService.update(etemp);
    }

    @Override
    public Shop selectByName(String name) {
        return null;
    }

    /**
     * 店铺审核通过
     * 1.修改状态state为1
     * 2.记录审核通过日志
     * 3.发送激活邮件
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        //1.修改状态state为待激活
        Shop shop = mapper.selectById(log.getShopId());
        shop.setState(1);
        mapper.update(shop);
//        //2.记录日志未完成(不理解这个日志表的意义,应该要怎么保存?是否需要在登录实现后带回session才能查到操作员id) TODO
//        log.setState(1);
//        log.setAuditId(351L);
//        shopAuditLogMapper.insert(log);
        //3.发送激活邮件,搞头填完注册表时,就已经修改了员工里面的shop_id,通过shopid直接拿到员工数据
        Employee employee = employeeMapper.selectByShopID(log.getShopId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("417976356@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻激活请求");
        //邮件内容
        mailMessage.setText("如果不是本人,请无视这封邮件;如果是注册人,请点击以下链接进行激活" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }

    /**
     * 店铺审核驳回
     * 1.修改状态state为1
     * 2.记录审核通过日志
     * 3.发送激活邮件
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        //1.修改状态state为-1
        Shop shop = mapper.selectById(log.getShopId());
        shop.setState(-1);
        mapper.update(shop);
        //2.记录日志未完成(不理解这个日志表的意义,应该要怎么保存?是否需要在登录实现后带回session才能查到操作员id) @TODO
        log.setState(-1);
        log.setAuditId(352L);//TODO 这是个假的数据
        shopAuditLogMapper.insert(log);
        //3.发送激活邮件,搞头填完注册表时,就已经修改了员工里面的shop_id,通过shopid直接拿到员工数据
        Employee employee = employeeMapper.selectByShopID(log.getShopId());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("417976356@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻激活请求已驳回");
        //邮件内容
        mailMessage.setText("如果不是本人,请无视这封邮件;如果是注册人,您填写的店铺信息有误，审核不通过，请尽快修改并重新提交审核" +
                "激活链接：http://localhost:8081/#/register2?shopId="+log.getShopId());
        //收件人
        mailMessage.setTo(employee.getEmail());
        javaMailSender.send(mailMessage);
    }

    /**
     * 邮件激活,修改状态
     * @param shopId
     */
    @Override
    public void activation(Long shopId) {
        Shop shop = mapper.selectById(shopId);
        shop.setState(2);
        mapper.update(shop);
    }

    /**
     * 批量新增
     * @param list
     */
    @Override
    public void patchInsert(List<Shop> list) {
        mapper.patchInsert(list);
    }

    /**
     * 驳回页面数据回显
     * @param id
     * @return
     */
    @Override
    public Shop reset(Long id) {
        Shop shop = mapper.selectById(id);
        return shop;
    }

    @Override
    public List<ShopVo> countByState() {
        return mapper.countByState();
    }
}
